<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>转账</title>
<!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
<link rel="stylesheet" href="/acct/css/bootstrap.min.css">
<script src="/acct/js/jquery.js" type="text/javascript"></script>
<script src="/acct/js/bootstrap.min.js" type="text/javascript"></script>
</head>
<body>
	<div style="width:50%; margin: 1px auto; align-content: center;">
		<h2  align="center">转账操作</h2>
	</div>
	<div style="width: 50%; margin: 100px auto;">
		<form>
		  <div class="form-group">
		    <label for="aAccount">转账者账号:</label>
		    <div id="aAccout-inupt-div">
			    <select class="form-control" id="aAccount">
				  <option>账户加载中···</option>
				</select>
			</div>
		  </div>
		  <div class="form-group">
		    <label for="bAccount">被转帐者账号:</label>
		    <input type="text" class="form-control" id="bAccount">
		  </div>
		  <div class="form-group">
		    <label  for="salary">转账金额：</label>
		    <div class="input-group">
		      <div class="input-group-addon">￥</div>
		      <input type="number" class="form-control" id="salary" placeholder="Amount">
		      <!-- <div class="input-group-addon">.00</div> -->
		    </div>
		  </div>
		  <div class="form-group">
			  <button style="float: right;" type="button" class="btn btn-primary" onclick="validateFormData()">转账</button>
			  <a href="/acct/main" style="float: right; margin-right: 20px" type="button" class="btn btn-default">回到首页</a>
			  	
		  </div>	  
		  
		</form>
	</div>

	<!-- Modal -->
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
	  <div class="modal-dialog" role="document">
	    <div class="modal-content">
	      <div class="modal-header">
	        
	        <h4 class="modal-title" id="myModalLabel">信息确认</h4>
	      </div>
	      <div class="modal-body">
	         <div class="modal-body">
				<form>
				  <div class="form-group">
				    <label for="aAccount-other">转账者账号:</label>
				    <input type="text" class="form-control" id="aAccount-other" readonly="readonly">
				  </div>
				  <div class="form-group">
				    <label for="bAccount-other">被转帐者账号:</label>
				    <input type="text" class="form-control" id="bAccount-other" readonly="readonly">
				  </div>
				  <div class="form-group">
				    <label for="bAccount-name">被转帐者姓名:</label>
				    <input type="text" class="form-control" id="bAccount-name" readonly="readonly">
				  </div>				  
				  <div class="form-group">
				    <label  for="salary-other">转账金额：</label>
				    <div class="input-group">
				      <div class="input-group-addon">￥</div>
				      <input type="number" class="form-control" id="salary-other" placeholder="Amount" readonly="readonly">
				    </div>
				  </div>	  
				</form>
		      </div>
	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
	        <button type="button" class="btn btn-primary" onclick="inputPwd()">确认转账</button>
	      </div>
	    </div>
	  </div>
	</div>

	<!-- Modal -->
	<div class="modal fade" id="pwdModal" tabindex="-1" role="dialog" aria-labelledby="pwdModalLabel">
	  <div class="modal-dialog" role="document">
	    <div class="modal-content">
	      <div class="modal-header">
	        
	        <h4 class="modal-title" id="pwdModallLabel">输入密码</h4>
	      </div>
	      <div class="modal-body">
	         <div class="modal-body">
				<form>
				  <div class="form-group">
				    <label for="password">请输入密码</label>
				    <input type="password" class="form-control" id="password">
				  </div>  
				</form>
		      </div>
	      </div>
	      <div class="modal-footer">
	        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
	        <button type="button" class="btn btn-primary" onclick="transfer()">确认转账</button>
	      </div>
	    </div>
	  </div>
	</div>

</body>



<script type="text/javascript">
	$(document).ready(function(){ 
		getUsreAccount();
	}); 
	function getUsreAccount(){
		var userid = <%= session.getAttribute("userId") %>;
		if(userid == null || userid == ''){
			var html = '<input type="text" class="form-control" ' 
					 + 'id="aAccount" placeholder="未找到用户编号，请尝试手动输入转账者账户">'
			$("#aAccout-inupt-div").html(html);
			return;
		}

		$.ajax({
		    type: "POST",
		    url: "/acct/transfer/listUseracct",
		    data: {"userId":userid},
		    dataType: "json",
		    success: function(data){
		               console.log(data);
		               var html = '';
		               var acctList = data.data;
		               if(data.code == '000000'){
		            	   for(var i=0; i<acctList.length; i++){
		            		   html += '<option value="' + acctList[i].acctNo + '">' + acctList[i].acctNo +'---余额 '+acctList[i].acctSalary+acctList[i].ccy+'</option>'
		            	   }
		            	   
		               }else{
		            	   html += '<option value="">未找到任何账户</option>';
		               }
		               $("#aAccount").html(html);
		    },
		    error: function(){
		    	alert('发生错误');
		    }
		});
	}
	
	
	function inputPwd(){
		$('#myModal').modal('toggle');
		$('#pwdModal').modal('toggle');
	}
	
	function checkInfo(){
		$.ajax({
		    type: "POST",
		    url: "/acct/transfer/findUsername",
		    data: {"acctNo":$("#bAccount").val()},
		    dataType: "json",
		    success: function(data){
		               if(data.code == '000000'){
		            	   $("#bAccount-name").val(data.data);
		            	   $("#aAccount-other").val($("#aAccount").val());
		            	   $("#bAccount-other").val($("#bAccount").val());
		            	   $("#salary-other").val($("#salary").val());

		            	   $('#myModal').modal('toggle');
		               }else if(data.code == '999333'){
		            	   alert('账号 ' + data.data + ' 不存在');
		               }else{
		            	   alert(data.msg);
		               }
		    }
		});
	}
	
	function transfer(){
		
		if(!validataPwd()){
			return;
		}
		
		// $('#pwdModal').modal('toggle');
		var formData = {};
		formData.aAcctNo = $("#aAccount").val();
		formData.bAcctNo = $("#bAccount").val();
		formData.salary = $("#salary").val();
		formData.password = $("#password").val();
		$('#password').val('');	
		
		$.ajax({
		    type: "POST",
		    url: "/acct/transfer/action",
		    data: formData,
		    dataType: "json",
		    success: function(data){
		    			if(data.code != '777777'){
		    				$('#pwdModal').modal('toggle');
		    			}
		               switch(data.code){
	               		case "000000":
	               			alert("转账成功.");
	               			window.location.href="/acct/page/transfer" 
               				break;
	               		case "999888":
	               			alert(data.msg);
	               			break;
	               		case "944444":
	               			// 余额不足
	               			alert("余额不足，转账失败.");
	               			break;
	               		default:
	               			alert(data.msg);
		               }
		    }
		});
	}
	
	function validataPwd(){
		var password = $("#password").val();
		if(password == null || password == ''){
			alert('请输入密码!');
			return false;
		}
		return true;
	}
	
	function validateFormData(){
		var aAcctNo = $("#aAccount").val();
		var bAcctNo = $("#bAccount").val();
		var salary = $("#salary").val();

		if(aAcctNo == null || aAcctNo == ''){
			alert("请输入转账账号!");
			return;
		}
		if(bAcctNo == null || bAcctNo == ''){
			alert("请输入转账账号!");
			return;
		}
		
		if(aAcctNo == bAcctNo){
			alert("转账账号与被转账账号请勿相同!");
			return;
		}
		
		if(salary == null || salary == 0.0){
			alert("请输入转账金额!");
			return;
		}
		checkInfo();
	}
	
</script>

</html>